﻿namespace EFS.RC.CC.Tools.Loggers
{
	public static class SAL
	{
		/// <summary>
		/// Add
		/// </summary>
		/// <param name="conversationGuid"></param>
		/// <param name="method"></param>
		/// <param name="context"></param>
		/// <returns></returns>
		public static System.DateTime Add(
			System.Guid? conversationGuid,
			System.String method,
			EFS.Common.ISelfDescriptor contextSelfDescriptor)
		{
			// check
			EFS.Common.Verify.Reference(contextSelfDescriptor, "contextSelfDescriptor");

			try
			{
				return (new EFS.RC.CC.SAL.Loggers.Messages.Message(conversationGuid, method, contextSelfDescriptor.Describe())).Occurred;
			}
			catch (System.Exception ex)
			{
 				// log
				EFS.EventLog.Log.LogException(ex, null);
			}

			// occurred
			return System.DateTime.Now;
		}

		/// <summary>
		/// Add
		/// </summary>
		/// <param name="conversationGuid"></param>
		/// <param name="method"></param>
		/// <param name="context"></param>
		/// <returns></returns>
		public static System.DateTime Add(
			System.Guid? conversationGuid,
			System.String method,
			System.String detail,
			EFS.Common.ISelfDescriptor contextSelfDescriptor)
		{
			// check
			EFS.Common.Verify.Reference(contextSelfDescriptor, "contextSelfDescriptor");

			try
			{
				return (new EFS.RC.CC.SAL.Loggers.Messages.Message(conversationGuid, method, detail, contextSelfDescriptor.Describe())).Occurred;
			}
			catch (System.Exception ex)
			{
				// log
				EFS.EventLog.Log.LogException(ex, null);
			}

			// occurred
			return System.DateTime.Now;
		}

		/// <summary>
		/// Add
		/// </summary>
		/// <param name="conversationGuid"></param>
		/// <param name="method"></param>
		/// <param name="context"></param>
		/// <returns></returns>
		public static System.DateTime Add(
			System.Guid? conversationGuid,
			System.String method,
			Microsoft.Rtc.Collaboration.ConversationState conversationState,
			EFS.Common.ISelfDescriptor contextSelfDescriptor)
		{
			// check
			EFS.Common.Verify.Reference(contextSelfDescriptor, "contextSelfDescriptor");

			try
			{
				return (new EFS.RC.CC.SAL.Loggers.Messages.Message(conversationGuid, method, ToString(conversationState), contextSelfDescriptor.Describe())).Occurred;
			}
			catch (System.Exception ex)
			{
				// log
				EFS.EventLog.Log.LogException(ex, null);
			}

			// occurred
			return System.DateTime.Now;
		}

		/// <summary>
		/// Add
		/// </summary>
		/// <param name="conversationGuid"></param>
		/// <param name="method"></param>
		/// <param name="context"></param>
		/// <returns></returns>
		public static System.DateTime Add(
			System.Guid? conversationGuid,
			System.String method,
			System.Exception exception,
			EFS.Common.ISelfDescriptor contextSelfDescriptor)
		{
			// check
			EFS.Common.Verify.Reference(contextSelfDescriptor, "contextSelfDescriptor");

			try
			{
				return (new EFS.RC.CC.SAL.Loggers.Messages.Message(conversationGuid, method, exception.Message, contextSelfDescriptor.Describe())).Occurred;
			}
			catch (System.Exception ex)
			{
				// log
				EFS.EventLog.Log.LogException(ex, null);
			}

			// occurred
			return System.DateTime.Now;
		}

		/// <summary>
		/// Add
		/// </summary>
		/// <param name="conversationGuid"></param>
		/// <param name="method"></param>
		/// <param name="context"></param>
		/// <returns></returns>
		public static System.DateTime Add(
			System.Guid? conversationGuid,
			System.String method,
			System.String uri,
			System.Type contextType)
		{
			// check
			EFS.Common.Verify.Reference(contextType, "contextType");

			try
			{
				return (new EFS.RC.CC.SAL.Loggers.Messages.Message(conversationGuid, method, uri, contextType.FullName)).Occurred;
			}
			catch (System.Exception ex)
			{
				// log
				EFS.EventLog.Log.LogException(ex, null);
			}

			// occurred
			return System.DateTime.Now;
		}

		/// <summary>
		/// Get conversation state desciption
		/// </summary>
		/// <param name="conversationState"></param>
		private static System.String ToString(Microsoft.Rtc.Collaboration.ConversationState conversationState)
		{
			// switch
			switch (conversationState)
			{
				case Microsoft.Rtc.Collaboration.ConversationState.Conferenced: return "Conferenced";
				case Microsoft.Rtc.Collaboration.ConversationState.Conferencing: return "Conferencing";
				case Microsoft.Rtc.Collaboration.ConversationState.Established: return "Established";
				case Microsoft.Rtc.Collaboration.ConversationState.Establishing: return "Establishing";
				case Microsoft.Rtc.Collaboration.ConversationState.Idle: return "Idle";
				case Microsoft.Rtc.Collaboration.ConversationState.Incoming: return "Incoming";
				case Microsoft.Rtc.Collaboration.ConversationState.InLobby: return "InLobby";
				case Microsoft.Rtc.Collaboration.ConversationState.Terminated: return "Terminated";
				case Microsoft.Rtc.Collaboration.ConversationState.Terminating: return "Terminating";
			}

			// no state
			return "";
		}
	}
}
